package noncomunicating.classes;

import java.util.Arrays;

import util.Regex;

import comunicating.classes.ServerLoop;

public class RequestAnalysis extends abstracts.Step{
	
	private void handleGET(String URL){
		
		if(URL.contains("?")){ // handles modifiers
			String SplitURL[] = URL.split("\\?");
			String mods[] = SplitURL[1].split("&");
			for( String e : mods){
				String tmpNode[] = e.split("=");
				if(tmpNode.length > 1){
					request.addModifier(tmpNode[0], tmpNode[1]); // Adds modifiers
				}
			}
			
			URL = SplitURL[0];
		}
		
		if(URL.endsWith("control.ctr")){
			request.setControl(true);
		}
		
		if(URL.endsWith("/")){// adds index.html to the directory
			URL += "index.html";
		}
		
		request.setFileString(ServerLoop.directory + URL);
	}
	
	private void handleAcceptLine(String[] httpRequestByLine){
		String acceptLine = "";
		for(String line : httpRequestByLine){// takes the accept type from the line
			if(line.startsWith("Accept:") || line.startsWith("accept:")){
				
				
				acceptLine = Regex.firstMatch("[a-zA-Z\\*]*/[a-zA-Z\\*]*", line);//XXX bad way of handling */*
				
			}
		}
		String[] dataType = acceptLine.split("/");//TODO find a way of verifying this data type
		
		request.setDataType(dataType);
	}
	
	private void populateHttpHashMap(){
		String[] lines = request.getHttpRequest().split("\n");
		for(String tmp : lines){
			if(tmp.contains("GET") || tmp.contains("HEAD")){
				continue;
			}
			else {
				String key = Regex.firstMatch("[a-zA-Z\\-\\_]*:", tmp).replace(":", ""); //gets everything before the first colon
				String value = Regex.firstMatch(":.*", tmp).replace(":", "");//gets everything after the first colon
				request.addHeaderDataItem(key, value);
			}
		}
	}

	public RequestAnalysis(Request request) {
		super(request);
		
		String httpString = request.getHttpRequest();
		String[] httpRequestByLine = httpString.split("\n");
		String URL = httpRequestByLine[0].split(" ")[1]; //entire middle section
		String httpMethod = httpRequestByLine[0].split(" ")[0];
		
		populateHttpHashMap();
		
		if(httpMethod.equalsIgnoreCase("GET")){
			handleGET(URL);
		}
		else if(httpMethod.equalsIgnoreCase("HEAD")){
			handleGET(URL);
			request.setHeadRequest(true);
		}
		
		handleAcceptLine(httpRequestByLine);
	}

	@Override
	public void printOutput(boolean verbose) {
		System.out.println(this.getOutput(verbose));
	}

	@Override
	public String getOutput(boolean verbose) {
		String retString = null;
		
		if(verbose){
			retString += request.getName() + ": HTTP Request Analyzed.\n";
			retString += "HEAD request: " + request.isHeadRequest() + "\n";
			retString += "Datatype: " + Arrays.toString(request.getDataType()) + "\n";
			retString += "Modifiers:" + request.getModifiers().toString() + "\n";
			retString += "File Location: " + request.getFileString() + "\n";
		}
		else{
			retString = "HTTP Request Analyzed.";
		}
		
		return retString;
	}
}
